// cl cat.c /std:c17

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
    if (argc < 2) {
        // no args provided
        fprintf(stdout, "Usage: %s <path>\n", argv[0]);
        return 1;
    }

    FILE* fp = fopen(argv[1], "r");
    if (!fp) {
        perror("fopen");
        return 1;
    }

    char buffer[1025] = {0};
    const int cbMaxRead = (sizeof(buffer) / sizeof(char)) - 1; // always 1024

    while (!feof(fp)) {
        size_t cbRead = fread(&buffer[0], sizeof(char), cbMaxRead, fp);
        if (cbRead != cbMaxRead) {
            if (ferror(fp)) {
                perror("fread");
                return 1;
            }
            // reach EOF
        }

        buffer[cbRead] = '\0';
        if (0 != fputs(buffer, stdout)) {
            perror("fputs");
            return 1;
        }
    }
    fclose(fp);
    return 0;
}

// https://en.cppreference.com/w/cpp/string/byte for "\0", C-style string

// https://en.cppreference.com/w/cpp/io/c/fopen
// https://en.cppreference.com/w/cpp/io/c/fclose

// https://en.cppreference.com/w/cpp/io/c/fread
// https://en.cppreference.com/w/cpp/io/c/feof

// https://en.cppreference.com/w/cpp/io/c/fprintf
// https://en.cppreference.com/w/cpp/io/c/fputs

// https://en.cppreference.com/w/cpp/io/c/ferror
// https://en.cppreference.com/w/cpp/io/c/perror
